package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysPlanVo;
import com.ruoyi.system.domain.bo.SysPlanBo;
import com.ruoyi.system.service.ISysPlanService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 规划设计Controller
 *
 * @author ruoyi
 * @date 2022-03-24
 */
@Validated
@Api(value = "规划设计控制器", tags = {"规划设计管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/plan")
public class SysPlanController extends BaseController {

    private final ISysPlanService iSysPlanService;

    /**
     * 查询规划设计列表
     */
    @ApiOperation("查询规划设计列表")
    @SaCheckPermission("system:plan:list")
    @GetMapping("/list")
    public TableDataInfo<SysPlanVo> list(@Validated(QueryGroup.class) SysPlanBo bo, PageQuery pageQuery) {
        return iSysPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出规划设计列表
     */
    @ApiOperation("导出规划设计列表")
    @SaCheckPermission("system:plan:export")
    @Log(title = "规划设计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysPlanBo bo, HttpServletResponse response) {
        List<SysPlanVo> list = iSysPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "规划设计", SysPlanVo.class, response);
    }

    /**
     * 获取规划设计详细信息
     */
    @ApiOperation("获取规划设计详细信息")
    @SaCheckPermission("system:plan:query")
    @GetMapping("/{planId}")
    public R<SysPlanVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("planId") Long planId) {
        return R.ok(iSysPlanService.queryById(planId));
    }

    /**
     * 新增规划设计
     */
    @ApiOperation("新增规划设计")
    @SaCheckPermission("system:plan:add")
    @Log(title = "规划设计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPlanBo bo) {
        return toAjax(iSysPlanService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改规划设计
     */
    @ApiOperation("修改规划设计")
    @SaCheckPermission("system:plan:edit")
    @Log(title = "规划设计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPlanBo bo) {
        return toAjax(iSysPlanService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除规划设计
     */
    @ApiOperation("删除规划设计")
    @SaCheckPermission("system:plan:remove")
    @Log(title = "规划设计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] planIds) {
        return toAjax(iSysPlanService.deleteWithValidByIds(Arrays.asList(planIds), true) ? 1 : 0);
    }

    /**
     * 删除规划设计
     */
    @ApiOperation("续建规划设计")
    @SaCheckPermission("system:plan:xujian")
    @Log(title = "规划设计", businessType = BusinessType.DELETE)
    @PostMapping("/xujian/{planIds}")
    public R<Void> xujian(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] planIds) {
        return toAjax(iSysPlanService.xujianByIds(Arrays.asList(planIds)));
    }
}
